package app.caihan.wanandroid.model.repository

import app.caihan.scframe.api.retrofit2.ExceptionHandle
import app.caihan.scframe.api.retrofit2.ScResult
import app.caihan.scframe.repository.BaseRepository
import app.caihan.wanandroid.model.api.WanApiResult
import app.caihan.wanandroid.model.api.WanService
import app.caihan.wanandroid.model.bean.BannerBean
import app.caihan.wanandroid.model.bean.Chapters
import app.caihan.wanandroid.model.bean.PageList
import app.caihan.wanandroid.model.bean.User
import app.caihan.wanandroid.model.db.WanDao
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

/**
 *
 *
 * @author Seven-C
 * @date 2020/8/17
 */
class WanRepositoryImpl private constructor(
    private val api: WanService,
    private val localData: WanDao
) : BaseRepository() {

    companion object {
        private val TAG = "WanRepositoryImpl"

        @Volatile
        private var INSTANCE: WanRepositoryImpl? = null

        fun getInstance(api: WanService, localData: WanDao) =
            INSTANCE ?: synchronized(this) {
                INSTANCE ?: WanRepositoryImpl(api, localData).also { INSTANCE = it }
            }
    }

    suspend fun getBanners(): List<BannerBean> = onlyResultSuccess { api.getBanners() }

    suspend fun getProjectList(page: Int = 0): PageList =
        onlyResultSuccess { api.getProjectList(page) }

    suspend fun getBanners1(): WanApiResult<List<BannerBean>> = api.getBanners()

    suspend fun getBanners2(): ScResult<List<BannerBean>> = requestScResult { api.getBanners() }
    suspend fun getArticleList2(page: Int = 0): ScResult<PageList> =
        requestScResult { api.getArticleList(page) }

    suspend fun getWxArticleChapters(): ScResult<List<Chapters>> =
        onlyResultCacheNetScResult(
            { api.getWxArticleChapters() },
            { localData.getWxArticleChapters() },
            {
                localData.deleteChaptersAll()
                localData.insertChapters(it)
            },
            { !it.isNullOrEmpty() }
        )

    suspend fun getWxArticleList(id: Int, page: Int = 1): ScResult<PageList> =
        requestScResult { api.getWxArticleList(id, page) }

    suspend fun getWxArticleList2(id: Int, page: Int = 1): WanApiResult<PageList> =
        api.getWxArticleList(id, page)

    suspend fun getBannersDb(isRefresh: Boolean = false): List<BannerBean> =
        onlyResultCacheNetSuccess(
            { api.getBanners() },
            { localData.getBannerList() },
            { localData.insertBanner(it) },
            { !isRefresh && !it.isNullOrEmpty() }
        )

    suspend fun getBannersDb2(isRefresh: Boolean = false): ScResult<List<BannerBean>> =
        withContext(Dispatchers.IO) {
            onlyResultCacheNetScResult(
                { api.getBanners() },
                { localData.getBannerList() },
                { localData.insertBanner(it) },
                { isRefresh && !it.isNullOrEmpty() }
            )
        }


    suspend fun login(userName: String, passWord: String): ScResult<User> =
        requestScResult { api.login(userName, passWord) }

    suspend fun getUserDb(): ScResult<User> = withContext(Dispatchers.IO) {
        try {
            val user = localData.getUserInfo()
            ScResult.Success(user)
        } catch (e: Exception) {
            ScResult.Error(ExceptionHandle.handleException(e))
        }
    }

    suspend fun setUserDb(user: User): ScResult<User> = withContext(Dispatchers.IO) {
        try {
            localData.insertUserInfo(user)
            ScResult.Success(user)
        } catch (e: Exception) {
            ScResult.Error(ExceptionHandle.handleException(e))
        }
    }

    suspend fun getTreeList(): ScResult<ArrayList<Chapters>> = requestScResult { api.getTreeList() }


}